<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div class="bacg">
    <el-card class="box-card">
      <div class="top">
        <img src="../assets/wallhaven-l89qp2.png" alt="" />
      </div>
      <el-form
        label-position="top"
        label-width="80px"
        :model="form"
        class="el-form"
        :rules="rules"
      >
        <el-form-item class="el-form-item" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入账号"
            prefix-icon="el-icon-mobile"
          ></el-input>
        </el-form-item>
        <el-form-item class="el-form-item" prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            x
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="domain" class="el-form-item one">
          <el-input
            v-model="form.domain"
            class="inptthree"
            placeholder="请输入验证码"
            prefix-icon="el-icon-key"
          ></el-input>
          <el-button class="btn">
            <img
              :src="this.domainImg"
              alt=""
              style="width: 90%; height: 80%"
              @click="adddomain"
            />
          </el-button>
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="login">登录</el-button>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getCaptcha } from '@/api/home'
import { Login } from '@/api/post'

export default {
  async created () {
    const res = await getCaptcha(this.form.clientToken)
    console.log(res)
    console.log(1)
    this.domainImg = `data:image/jpeg;base64,${btoa(
      new Uint8Array(res).reduce(
        (data, byte) => data + String.fromCharCode(byte),
        ''
      )
    )}`
    console.log(this.domainImg)
    console.log(this.form.clientToken)
  },
  data () {
    return {
      domainImg: '',

      form: {
        username: 'admin',
        password: 'admin',
        domain: '',
        clientToken: Math.floor(Math.random() * 10000) + 1
      },
      rules: {
        username: [
          { required: true, message: '请输入名称', trigger: 'change' },
          {
            min: 3,
            max: 11,
            message: '长度在 3 到 11 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          {
            min: 6,
            max: 11,
            message: '长度在 6 到 11 个字符',
            trigger: 'blur'
          }
        ],
        domain: [
          { required: true, message: '请输入验证码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    async login () {
      console.log(this.form.domain, this.form.clientToken)
      const res = await Login(
        {
          loginName: this.form.username,
          password: this.form.password,
          code: this.form.domain,
          loginType: 0,
          clientToken: this.form.clientToken
        }

      )
      console.log(res.token)
      this.$store.commit('user/setUserToken', res.token)
      // console.log(this.form.domain, this.form.clientToken)
      if (res.success) {
        this.$router.push('/home')
      }
    },
    async adddomain () {
      const res = await getCaptcha()
      console.log(res)
      this.domainImg = `data:image/jpeg;base64,${btoa(
        new Uint8Array(res).reduce(
          (data, byte) => data + String.fromCharCode(byte),
          ''
        )
      )}`
    }
  }
}
</script>

<style lang="scss" scoped>
.bacg {
  min-height: 100vh;
  background: url("../assets/img.jpg") no-repeat center / cover;
  display: flex;
  justify-content: space-around;
  align-items: center;
  .box-card {
    position: relative;
    width: 518px;
    height: 388px;
    overflow: inherit;
    margin: 240px auto;
    .top {
      position: absolute;
      margin-left: 50%;
      transform: translateX(-50%);
      margin-top: -10%;
      img {
        width: 90px;
      }
    }
    .el-form {
      margin-top: 15%;
      button {
        width: 100%;
        height: 45px;
        background-color: #4e69e7;
      }
      .el-form-item {
        margin-bottom: 25px;

        button {
          height: 50px;
          width: 123px;
          line-height: 100%;
        }
      }
      .one {
        width: 100%;
        display: flex;
        justify-content: space-evenly;
      }
      .inptthree {
        width: 348px;
        height: 50px;
      }
      .btn {
        background: none;
        padding: 0;
        border: none;
        transform: translateY(30%);
      }
    }
  }
}
</style>
